﻿package net.wangit.email;

import net.wangit.Path;
import net.wangit.framework.exception.ConfigurationException;
import net.wangit.util.FileUtil;
import net.wangit.util.XMLUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;


public class EmailSettings {

	private static Log log;
	private static String path;
	private static String host;
	private static String user;
	private static String password;
	private static String charset;

	public EmailSettings() {
	}

	public static String getHost() {
		return host;
	}

	public static String getUser() {
		return user;
	}

	public static String getPassword() {
		return password;
	}

	public static String getCharset() {
		return charset;
	}

	static {
		log = LogFactory.getLog(net.wangit.email.EmailSettings.class);
		path = (new StringBuilder(String.valueOf(Path.getHomePath()))).append(
				"/email.xml").toString();
		try {
			if (!FileUtil.exists(path)) {
				log.error((new StringBuilder("email file not found:")).append(
						path).toString());
				throw new ConfigurationException("email file not found!");
			}
			Document document = XMLUtil.parseByPath(path);
			org.dom4j.Element root = document.getRootElement();
			host = XMLUtil.getTextString(root, "host", "");
			user = XMLUtil.getTextString(root, "user", "");
			password = XMLUtil.getTextString(root, "password", "");
			charset = XMLUtil.getTextString(root, "charset", "");
		} catch (Exception e) {
			log.error("load sms settings failed!");
			throw new ConfigurationException(path, e);
		}
		log.info("load sms settings successfully.");
	}
}
